<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-编辑</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<div class="easy-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <input type="text" name="id" th:field="${data.id}" class="layui-input" style="display: none">
        <div class="layui-row">
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label required">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" th:field="${data.title}" lay-verify="required" placeholder="请输入" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label required">类型</label>
                    <div class="layui-input-block">
                        <select name="type" th:field="${data.type}" lay-verify="required" th:with="dataList=${@dictService.listData('MENU_TYPE')}">
                            <option th:each="dict : ${dataList}" th:text="${dict.dictItem}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label ">父级菜单</label>
                    <div class="layui-input-block">
                        <input id="parentId" name="parentId" th:field="${data.parentId}" class="layui-input" style="display: none;">
                        <input id="parentName" name="parentName" type="text" th:field="${data.parentName}" placeholder="请选择" autocomplete="off" class="layui-input" readonly="readonly">
                        <div id="menuTree" style="display: none"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label ">图标</label>
                    <div class="layui-input-block">
                        <input type="text" name="icon" th:field="${data.icon}"  placeholder="请输入" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label ">路由</label>
                    <div class="layui-input-block">
                        <input type="text" name="route" th:field="${data.route}"  placeholder="请输入" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label ">编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="code" th:field="${data.code}"  placeholder="请输入" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label required">排序号</label>
                    <div class="layui-input-block">
                        <input type="number" name="orderNum" th:field="${data.orderNum}" lay-verify="required" autocomplete="off" class="layui-input" step="1" lay-affix="number">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label ">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark" th:field="${data.remark}"  placeholder="请输入" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="cancelForm">取消</button>
            </div>
        </div>
    </form>
</div>
</body>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'layer', 'table', 'tree', 'laydate', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var tree = layui.tree;
        var laydate = layui.laydate;
        var form = layui.form;

        var selectNode = {};
        $("#parentName").click(function () {
            layer.open({
                type: 1,
                title: "选择菜单",
                area: ['400px', '300px'],
                shade: 0,
                shadeClose: false,
                content: $("#menuTree"),
                btn: ['确定', '取消'],
                success: function () {
                    loadMenuTree();
                },
                yes: function (index) {
                    if (selectNode.data != null) {
                        // 选中回显
                        form.val('dataForm', {
                            "parentId": selectNode.data.id,
                            "parentName": selectNode.data.title,
                        });
                    }
                    layer.close(index);
                }
            });
        });

        var loadMenuTree = function () {
            $.ajax({
                url: '/sysMenu/list',
                type: 'GET',
                contentType: 'application/json',
                dataType: 'json',
                success: function (response) {
                    if (response.code === 200) {
                        tree.render({
                            elem: '#menuTree',
                            data: response.data,
                            onlyIconControl: true,
                            click: function (obj) {
                                selectNode = obj;

                                layer.msg("当前选中：" + selectNode.data.title);
                            }
                        });
                    }
                },
                error: function (xhr, status, error) {
                }
            });
        };

        // 保存
        form.on('submit(submitForm)', function (form) {
            var data = form.field;
            $.ajax({
                url: '/sysMenu/edit',
                type: 'PUT',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (response) {
                    if (response.code === 200) {
                        // 关闭
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        // 刷新
                        parent.location.reload();

                        layer.msg('编辑成功', {icon: 1});
                    } else {
                        layer.msg(response.msg, {icon: 0});
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('请求失败', {icon: 0});
                }
            });
            return false;
        });

        // 取消
        $('#cancelForm').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });

    });
</script>
</html>